package com.wzt.lianfirecontrol.fragment.yuyueweibao;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.yuyueweibao.YuYueModel;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import java.util.HashMap;

public class YuYueEvaluateFragment extends BaseFragment {
    private BaseActivity activity;
    private View itemContentView;
    private Bundle bundle;

    private TextView tv_user_name;
    private TextView tv_user_phone;

    private ImageView iv_star_1;
    private ImageView iv_star_2;
    private ImageView iv_star_3;
    private ImageView iv_star_4;
    private ImageView iv_star_5;

    private EditText et_remark;
    private Button bt_commit;

    private View include_progress_bar;

    private YuYueModel yuYueModel;

    private int currentStarNum = 0;


    private static final int SUBMIT_WHAT = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            activity = getMyActivity();
            bundle = getArguments();
            if (null != bundle){
                yuYueModel = (YuYueModel) bundle.getSerializable("yuyueModel");
            }
            itemContentView = inflater.inflate(R.layout.f_yuyue_evaluate, container, false);
            initView();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        tv_user_name = itemContentView.findViewById(R.id.tv_user_name);
        tv_user_name.setText(yuYueModel.getRealName());
        tv_user_phone = itemContentView.findViewById(R.id.tv_user_phone);
        tv_user_phone.setText(yuYueModel.getPhone());

        iv_star_1 = itemContentView.findViewById(R.id.iv_star_1);
        iv_star_2 = itemContentView.findViewById(R.id.iv_star_2);
        iv_star_3 = itemContentView.findViewById(R.id.iv_star_3);
        iv_star_4 = itemContentView.findViewById(R.id.iv_star_4);
        iv_star_5 = itemContentView.findViewById(R.id.iv_star_5);

        iv_star_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentStarNum = 1;
                updateStar();
            }
        });

        iv_star_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentStarNum = 2;
                updateStar();
            }
        });

        iv_star_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentStarNum = 3;
                updateStar();
            }
        });

        iv_star_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentStarNum = 4;
                updateStar();
            }
        });

        iv_star_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentStarNum = 5;
                updateStar();
            }
        });


        et_remark = itemContentView.findViewById(R.id.et_remark);
        bt_commit = itemContentView.findViewById(R.id.bt_commit);
        bt_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initCommitHttpHelper();
            }
        });

        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
    }

    private void updateStar() {
        iv_star_1.setImageResource(R.mipmap.icon_star_bad);
        iv_star_2.setImageResource(R.mipmap.icon_star_bad);
        iv_star_3.setImageResource(R.mipmap.icon_star_bad);
        iv_star_4.setImageResource(R.mipmap.icon_star_bad);
        iv_star_5.setImageResource(R.mipmap.icon_star_bad);
        switch (currentStarNum){
            case 5:
                iv_star_5.setImageResource(R.mipmap.icon_star_good);
            case 4:
                iv_star_4.setImageResource(R.mipmap.icon_star_good);
            case 3:
                iv_star_3.setImageResource(R.mipmap.icon_star_good);
            case 2:
                iv_star_2.setImageResource(R.mipmap.icon_star_good);
            case 1:
                iv_star_1.setImageResource(R.mipmap.icon_star_good);
        }
    }

    private void initCommitHttpHelper() {
        try {
            if (!Utils.hasNetwork(activity)){
                ToastUtils.showToast(activity, "网络未连接");
                return;
            }
            include_progress_bar.setVisibility(View.VISIBLE);
            HashMap<String, Object> hashMap = new HashMap<>();
            if (currentStarNum == 0){
                ToastUtils.showToast(activity, "请做出评价");
                return;
            }else {
                hashMap.put("star", currentStarNum + "");
            }
            hashMap.put(ConstData.COMPANYID, UserInfoModel.getCompandId(activity));
            hashMap.put("remark", et_remark.getText().toString());
            hashMap.put("repairCompanyId", yuYueModel.getRepairCompanyId());
            hashMap.put("repairNum", yuYueModel.getNum());
            hashMap.put("taskId", yuYueModel.getId());
            hashMap.put("userId", yuYueModel.getUserId());

            HashMap<String, String> url_params = new HashMap<>();
            url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
            DetailHttpHelper detailHttpHelper = new DetailHttpHelper(activity, Url.YUYUE_COMMIT_RAISE, SUBMIT_WHAT, handler, true, url_params);
            detailHttpHelper.setParseJsonData(new LoginParseJsonData());
            detailHttpHelper.getHttpRequest(true);
        }catch (Exception e){}
    }

    class DetailHttpHelper extends HttpHelper {

        public DetailHttpHelper(BaseActivity activity, String url, int what, Handler handler, boolean isPost, HashMap<String, String> params) {
            super(activity, url, what, handler, isPost, params);
        }

    }

    protected class LoginParseJsonData extends ParseJsonData {

        public LoginParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            switch (what){
            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if (!bundle.getBoolean(ParseJsonData.REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
            if (ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))) {
                if (msg.what == SUBMIT_WHAT) {
                    PreferencesUtils.putString(activity, ConstData.NEEDREFRESH, "0");
                    activity.finish();
                }else {
                    include_progress_bar.setVisibility(View.GONE);
                }
            }else {
                include_progress_bar.setVisibility(View.GONE);
            }
            return;
        }

    }

}
